import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


/**
 * GameEngine Class runs the game and deals with all windows and controllers
 * @author Team Love is Like Sand
 * @version 1.0
 *
 */
public class GameEngine {
	
	/**
	 * Player in the game
	 */
	private Player player;
	
	/**
	 * Universe for the game
	 */
	private Universe uni;
	
	/**
	 * Map for the game
	 */
	private UniverseMap map;
	
	/**
	 * Controller for the map
	 */
	private UniverseMapController control;
	
	/**
	 * Main method to run the program
	 * @param args
	 */
	public static void main(String[] args) {
		IntroWindow intro = new IntroWindow();
		try{
			InputStream in = new FileInputStream(new File("audio.wav"));
			AudioStream as = new AudioStream(in);
			AudioPlayer.player.start(as);
		}
		catch(IOException e){
			System.err.println("No audio");
		}
		
		
		while(!intro.isLoaded()){
			try {
			Thread.sleep(200);
			}
			catch(InterruptedException e){
				e.getStackTrace();
			}
		}
		if(intro.getState() == 0){
			GameEngine engine = new GameEngine();
		}
		else{
			GameEngine engine = new GameEngine(intro.getPlayer(), intro.getUniverse());
		}
	}

	/**
	 * Constructor for GameEngine Class
	 */
	public GameEngine(){
		CharacterCreationController controller = new CharacterCreationController();
		CharacterCreationWindow creationWindow = new CharacterCreationWindow(controller);
		uni = new Universe();
	
		while(player == null){
			try {
			Thread.sleep(50);
			}
			catch( InterruptedException e){
				e.getStackTrace();
			}
			player = controller.getPlayer();
		}
		player.setSystem(uni.getSystems()[0]);
		play();
	}
	
	/**
	 * Constructor for GameEngine Class
	 * @param p player that is set
	 * @param u universe that is set
	 */
	public GameEngine(Player p, Universe u){
		player = p;
		uni = u;
		play();
	}
	
	/**
	 * Sets up the game ready to play with a player
	 */
	public void play(){
		StatusBar.setPlayer(player);
		control = new UniverseMapController(player);
		map = new UniverseMap(uni, control);
	}
}
